/*
Copyright 2021 LuJun

Permission is hereby granted, free of charge, to any person obtaining a copy 
of this software and associated documentation files (the "Software"), to deal 
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
copies of the Software, and to permit persons to whom the Software is furnished 
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { error, ImageAsset, SpriteFrame, Texture2D, __private } from 'cc';
import { pipeline } from './pipeline';

const TAG = 'lcc/core/pipeline-preset.ts';

/**
 * @zh
 * 字符串管道
 * any => string
 * 把输入数据转换为字符串文本
 *
 * @param input 输入数据
 * @returns 字符串文本
 */
async function StringPipeline(input: any) {
    if (input === null || input === undefined) {
        return null;
    }
    if (typeof input === 'object') {
        return JSON.stringify(input);
    } else {
        return String(input);
    }
}

pipeline.register('string', StringPipeline);
pipeline.register('text', StringPipeline);

/**
 * @zh
 * 对象管道
 * object | string => object
 * 把输入数据转换为对象
 *
 * @param input 输入数据
 * @returns 转换后的对象
 */
async function ObjectPipeline(input: object | string) {
    if (input === null || input === undefined) {
        return null;
    }
    if (typeof input === 'object') {
        return input;
    } else if (typeof input === 'string') {
        try {
            let result = JSON.parse(input);
            if (typeof result !== 'object') {
                error(TAG, `ObjectPipeline input '${input}' can not be converted to object`);
                return null;
            }
            return result;
        } catch (e) {
            error(TAG, `ObjectPipeline input '${input}' is not json string`);
            return null;
        }
    } else {
        error(TAG, 'ObjectPipeline input type unknown');
        return null;
    }
}

pipeline.register('object', ObjectPipeline);
pipeline.register('json', ObjectPipeline);

/**
 * Texture管道
 * ImageAsset | TextureBase | SpriteFrame => TextureBase
 *
 * @param input 输入数据
 * @returns Texture对象
 */
async function TexturePipeline(
    input: ImageAsset | __private._cocos_core_assets_texture_base__TextureBase | SpriteFrame
) {
    if (input === null || input === undefined) {
        return null;
    }
    if (input instanceof __private._cocos_core_assets_texture_base__TextureBase) {
        return input;
    } else if (input instanceof ImageAsset) {
        let texture = new Texture2D();
        texture.image = input;
        return texture;
    } else if (input instanceof SpriteFrame) {
        return input.texture;
    } else {
        error(TAG, 'TexturePipeline input type unknown');
        return null;
    }
}

pipeline.register('texture', TexturePipeline);

/**
 * @zh
 * SpriteFrame管道
 * ImageAsset | Texture2D | SpriteFrame => SpriteFrame
 *
 * @param input 输入数据
 * @returns SpriteFrame对象
 */
async function SpriteFramePipeline(input: ImageAsset | Texture2D | SpriteFrame) {
    if (input === null || input === undefined) {
        return null;
    }
    if (input instanceof SpriteFrame) {
        return input;
    } else if (input instanceof ImageAsset) {
        let texture = new Texture2D();
        texture.image = input;
        let spriteFrame = new SpriteFrame();
        spriteFrame.texture = texture;
        return spriteFrame;
    } else if (input instanceof Texture2D) {
        let spriteFrame = new SpriteFrame();
        spriteFrame.texture = input;
        return spriteFrame;
    } else {
        error(TAG, 'SpriteFramePipeline input type unknown');
        return null;
    }
}

pipeline.register('spriteframe', SpriteFramePipeline);
